package com.bio.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.bio.entity.attachemnt.Attachment;
import com.bio.service.attachment.AttachmentService;


public class AttachmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public AttachmentServlet() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		AttachmentService attachmentService = (AttachmentService)ctx.getBean("attachmentService");
		String attachmentId = request.getParameter("id");
		Attachment attachment = attachmentService.get(Long.valueOf(attachmentId));
		int fileSize = attachment.getSize();
		String fileContentType = attachment.getFileContentType();
		response.setContentLength(fileSize);
		response.setContentType(fileContentType);
		java.io.OutputStream out = response.getOutputStream();
		out.write(attachment.getData());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
